package com.personal.book.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.personal.book.dao.OrdersDao;
import com.personal.book.entity.Book;
import com.personal.book.entity.Orders;
import com.personal.tools.DBTools;
import com.personal.tools.PageData;

import sun.security.pkcs11.Secmod.DbMode;

/**
 * Orders接口的实现类
 * 
 * @author 姬
 *
 */
public class OrdersDaoImpl implements OrdersDao {

	// 生成订单
	@Override
	public boolean AddOrders(Orders order) {
		// TODO Auto-generated method stub
		String sql = "insert into orders values(?,?,?,?)";
		return DBTools.exUpdate(sql, order.getOrderId(), order.getOrderDate(), order.getUserName(),
				order.getAllPrice()) > 0;
	}

	@Override
	public boolean delOrders(int orderId) {
		// TODO Auto-generated method stub
		String sql = "delete from orders where orderId=?";
		return DBTools.exUpdate(sql, orderId) > 0;
	}

	// 根据用户名查询订单
	@Override
	public PageData<Orders> getOrdersByName(int page, int pageSize, String userName) {
		// TODO Auto-generated method stub
		String sql = "select * from orders where userName like ?";
		PageData<Orders> pd = DBTools.exQueryByPage(sql, Orders.class, page, pageSize, '%' + userName + '%');
		return pd;
	}

	// 分页 根据下单的日期的区间进行查询
	@SuppressWarnings("unchecked")
	@Override
	public PageData<Orders> getOrdersByDate(int page, int pageSize, String max, String min) {
		// TODO Auto-generated method stub

		PageData<Orders> pd = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh:mm");
		Date minDate;
		Date maxDate;
		try {
			minDate = sdf.parse(min);
			maxDate = sdf.parse(max);
			if (minDate.getTime() == maxDate.getTime()) {
				String sql = "select * from orders where orderDate = ? ";
				pd = DBTools.exQueryByPage(sql, Book.class, page, pageSize, minDate);
			} else if (maxDate.getTime() > minDate.getTime()) {
				String sql = "select * from orders where orderDate >= ? and orderDate <= ? ";
				pd = DBTools.exQueryByPage(sql, Book.class, page, pageSize, minDate, maxDate);
			} else {
				return null;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return pd;
	}

	// 分页 根据订单总价格的范围进行查询
	@SuppressWarnings("unchecked")
	@Override
	public PageData<Orders> getOrdersByPrice(int page, int pageSize, double max, double min) {
		// TODO Auto-generated method stub
		PageData<Orders> pd = null;
		if (max == min) {
			String sql = "select * from orders where allPrice = ? ";
			pd = DBTools.exQueryByPage(sql, Book.class, page, pageSize, min);
		} else if (max > min) {
			String sql = "select * from orders where allPrice >= ? and allPrice <= ? ";
			pd = DBTools.exQueryByPage(sql, Book.class, page, pageSize, min, max);
		} else {
			return null;
		}
		return pd;
	}

	// 根据订单总价进行升序
	@SuppressWarnings("unchecked")
	@Override
	public PageData<Orders> getOrdersByAsc(int page, int pageSize) {
		// TODO Auto-generated method stub
		String sql = "select * from orders order by allPrice asc";
		return DBTools.exQueryByPage(sql, Orders.class, page, pageSize);
	}

	// 根据订单总价进行降序
	@SuppressWarnings("unchecked")
	@Override
	public PageData<Orders> getOrdersByDesc(int page, int pageSize) {
		// TODO Auto-generated method stub
		String sql = "select * from orders order by allPrice desc";
		return DBTools.exQueryByPage(sql, Orders.class, page, pageSize);
	}

	// 根据订单编号查询
	@Override
	public List<Orders> getOrdersById(int orderId) {
		// TODO Auto-generated method stub
		String sql = "select * from orders where orderId = ?";
		return (List<Orders>) DBTools.exQuery(sql, Orders.class, orderId);
	}
	@Override
	public List<Orders> getOrdersByName(String userName) {
		// TODO Auto-generated method stub
		String sql = "select * from orders where userName = ?";
		 return (List<Orders>) DBTools.exQuery(sql, Orders.class, userName);
	}
}
